package com.kingzoon.seckill.service.impl.account;

import java.util.HashMap;
import java.util.List;

import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import ch.qos.logback.classic.Logger;
import payment.tools.util.GUID;
import payment.tools.util.TimeUtil;

import com.kingzoon.seckill.dao.UaLogLoginMapper;
import com.kingzoon.seckill.dao.UaLoginMethodMapper;
import com.kingzoon.seckill.dao.UaMemberInfoMapper;
import com.kingzoon.seckill.dto.Result;
import com.kingzoon.seckill.entity.UaLogLogin;
import com.kingzoon.seckill.entity.UaLoginMethod;
import com.kingzoon.seckill.entity.UaLoginMethodExample;
import com.kingzoon.seckill.entity.UaMemberInfo;
import com.kingzoon.seckill.entity.UaMemberInfoExample;
import com.kingzoon.seckill.enums.MacroDefine;
import com.kingzoon.seckill.service.MemberLoginService;
import com.kingzoon.seckill.service.impl.base.PasswordHelper;
import com.kingzoon.seckill.util.UtilTools;

/**
 * 会员登陆
 * @author raymond
 *
 */
@Service
public class MemberLoginServiceImpl implements MemberLoginService {
	
	private Logger log = (Logger) LoggerFactory.getLogger(MemberLoginServiceImpl.class);
	
	private HashMap<String,Object> paramMap = new HashMap<String,Object>();
	
	@Autowired
	private UaMemberInfoMapper uaMemberInfoDao;
	
	@Autowired
	private UaLoginMethodMapper uaLoginMethodDao;
	
	@Autowired
	private UaLogLoginMapper uaLogLoginDao;
	
	@Autowired
	private PasswordHelper passwordHelper;
	
	private UaMemberInfo uaMemberInfo = new UaMemberInfo();
	private UaLoginMethod uaLoginMethod = new UaLoginMethod();
	private UaLogLogin uaLogLogin = new UaLogLogin();
	private UaMemberInfoExample uaMemberInfoExample = new UaMemberInfoExample();
	private UaLoginMethodExample uaLoginMethodExample = new UaLoginMethodExample();

	public Result<HashMap<String, Object>> doBusiness(HashMap<String, Object> mapParam, Result<HashMap<String, Object>> rs) {
		paramMap = mapParam;
		if(preCheck(rs)){
			if(cache(rs)){
				if(check(rs)){
					if(before(rs)){
						if(clear(rs)){
							after(rs);
						}
					}
				}
			}
		}
		return rs;
	}

	public boolean preCheck(Result<HashMap<String, Object>> rs) {
		String[] keys = {"phoneNo","password"};
		String emptyString = UtilTools.isParamEmpty(keys, paramMap);
		if(!UtilTools.isEmpty(emptyString)){
			rs.setFlag(false);
			rs.setCode(MacroDefine.GS_CODE.E_1002.getCode());
			rs.setMessage(MacroDefine.GS_CODE.E_1002.getMessage());
			return false;
		}
		return true;
	}

	public boolean cache(Result<HashMap<String, Object>> rs) {
		return true;
	}


	public boolean check(Result<HashMap<String, Object>> rs) {
		//检查登陆方法表
		uaLoginMethodExample.createCriteria().andLoginNameEqualTo(UtilTools.valueOf(paramMap.get("phoneNo")));
		List<UaLoginMethod> methodList = uaLoginMethodDao.selectByExample(uaLoginMethodExample);
		if(methodList == null || methodList.size() <= 0){
			rs.setFlag(false);
			rs.setCode(MacroDefine.ZH_CODE.E_5400.getCode());
			rs.setMessage(MacroDefine.ZH_CODE.E_5400.getMessage());
			return false;
		}
		uaLoginMethod = methodList.get(0);
		//检查注册会员表
		uaMemberInfoExample.createCriteria().andMemberMobileEqualTo(UtilTools.valueOf(paramMap.get("phoneNo")));
		List<UaMemberInfo> memberList = uaMemberInfoDao.selectByExample(uaMemberInfoExample);
		if(memberList != null && memberList.size() > 0){
			uaMemberInfo =  memberList.get(0);
			String lockStatus = uaMemberInfo.getLockStatus();
			//判断账户是否锁定状态
			if("1".equals(lockStatus)){
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5406.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5406.getMessage());
				return false;
			}
		}
		return true;
	}

	public boolean before(Result<HashMap<String, Object>> rs) {
		paramMap.put("exchangeId", uaLoginMethod.getExchangeId());
		paramMap.put("distributorId", uaLoginMethod.getDistributorId());
		paramMap.put("memberId", uaLoginMethod.getMemberId());
		paramMap.put("loginSerialNo", GUID.getTxNo());
		paramMap.put("loginName", uaLoginMethod.getLoginName());
		paramMap.put("loginType", paramMap.get("fromWay"));
		paramMap.put("loginDate", TimeUtil.getTimeStamp().substring(0, 8));
		paramMap.put("loginTime", TimeUtil.getTimeStamp().substring(8));
		paramMap.put("loginPwd", paramMap.get("password"));
		paramMap.put("errorNum", UtilTools.isNull(uaMemberInfo.getErrorNum()) ? 0 : uaMemberInfo.getErrorNum().intValue());
		paramMap.put("errorNumTotal", UtilTools.isNull(uaMemberInfo.getErrorNumTotal()) ? 0 : uaMemberInfo.getErrorNumTotal().intValue());
		paramMap.put("loginIp", paramMap.get("loginIp"));
		paramMap.put("curLoginDate", paramMap.get("loginDate"));
		paramMap.put("curLoginTime", paramMap.get("loginTime"));
		paramMap.put("oldLoginDate", UtilTools.isNull(uaLoginMethod.getCurLoginDate()) ? Long.valueOf((String)paramMap.get("loginDate")) : uaLoginMethod.getCurLoginDate());
		paramMap.put("oldLoginTime", UtilTools.isNull(uaLoginMethod.getCurLoginTime()) ? Long.valueOf((String)paramMap.get("loginTime")) : uaLoginMethod.getCurLoginTime());
		paramMap.put("curLoginIp", paramMap.get("loginIp"));
		paramMap.put("oldLoginIp", uaLoginMethod.getCurLoginIp());
		paramMap.put("loginFromWay", paramMap.get("fromWay"));
		paramMap.put("loginNum", UtilTools.isNull(uaLoginMethod.getLoginNum()) ? 0 : uaLoginMethod.getLoginNum());
		paramMap.put("pwdErrorNum", UtilTools.isNull(uaLoginMethod.getPwdErrorNum()) ? 0 : uaLoginMethod.getPwdErrorNum());
		return true;
	}

	@Transactional
	public boolean clear(Result<HashMap<String, Object>> rs) {
		String onlineSatatus = "1";//0：离线，1：在线
		//登录密码校验
		String loginPwd = uaMemberInfo.getLoginPwd();
		
		if(!loginPwd.equals(UtilTools.valueOf(paramMap.get("loginPwd")))){
			onlineSatatus = "0";
			uaLoginMethod.setPwdErrorNum((Long)paramMap.get("pwdErrorNum") + 1);
			int c = uaLoginMethodDao.updateByPrimaryKeySelective(uaLoginMethod);
			if(c != 1){
				rs.setFlag(false);
				rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
				rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
				return false;
			}
			uaMemberInfo.setLockStatus((Integer)paramMap.get("errorNum") == 3 ? "1" : "0");
			uaMemberInfo.setErrorNum(Short.valueOf(String.valueOf((Integer)paramMap.get("errorNum")+1)));
			uaMemberInfo.setErrorNumTotal((long)((Integer)paramMap.get("errorNumTotal") + 1));
			uaMemberInfoExample.createCriteria().andMemberMobileEqualTo(UtilTools.valueOf(paramMap.get("phoneNo")));
			int d = uaMemberInfoDao.updateByExampleSelective(uaMemberInfo, uaMemberInfoExample);
			if(d != 1){
				rs.setFlag(false);
				rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
				rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
				return false;
			}
			if((Integer)paramMap.get("errorNum") == 0){
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5403.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5403.getMessage());
			}else if((Integer)paramMap.get("errorNum") == 1){
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5404.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5404.getMessage());
			}else if((Integer)paramMap.get("errorNum") == 2){
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5405.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5405.getMessage());
			}else{
				rs.setFlag(false);
				rs.setCode(MacroDefine.ZH_CODE.E_5406.getCode());
				rs.setMessage(MacroDefine.ZH_CODE.E_5406.getMessage());
			}
			return false;
		}
		
		//更新登陆信息表
		uaLoginMethod.setCurLoginDate(Long.valueOf(UtilTools.valueOf(paramMap.get("curLoginDate"))));
		uaLoginMethod.setCurLoginTime(Long.valueOf(UtilTools.valueOf(paramMap.get("curLoginTime"))));
		uaLoginMethod.setOldLoginDate((Long)paramMap.get("oldLoginDate"));
		uaLoginMethod.setOldLoginTime((Long)paramMap.get("oldLoginTime"));
		uaLoginMethod.setCurLoginIp(UtilTools.valueOf(paramMap.get("curLoginIp")));
		uaLoginMethod.setOldLoginIp(UtilTools.valueOf(paramMap.get("oldLoginIp")));
		uaLoginMethod.setLoginFromWay(UtilTools.valueOf(paramMap.get("loginFromWay")));
		uaLoginMethod.setLoginNum((Long)paramMap.get("loginNum") + 1);
		uaLoginMethod.setOnlineStatus(onlineSatatus);
		int a = uaLoginMethodDao.updateByPrimaryKeySelective(uaLoginMethod);
		if(a != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		
		//写入登陆日志表
		uaLogLogin.setExchangeId(UtilTools.valueOf(paramMap.get("exchangeId")));
		uaLogLogin.setDistributorId(UtilTools.valueOf(paramMap.get("distributorId")));
		uaLogLogin.setMemberId(UtilTools.valueOf(paramMap.get("memberId")));
		uaLogLogin.setLoginSerialno(UtilTools.valueOf(paramMap.get("loginSerialNo")));
		uaLogLogin.setLoginName(UtilTools.valueOf(paramMap.get("loginName")));
		uaLogLogin.setLoginType(UtilTools.valueOf(paramMap.get("loginType")));
		uaLogLogin.setLoginDate(Long.valueOf(UtilTools.valueOf(paramMap.get("loginDate"))));
		uaLogLogin.setLoginTime(Long.valueOf(UtilTools.valueOf(paramMap.get("loginTime"))));
		uaLogLogin.setLoginIp(UtilTools.valueOf(paramMap.get("loginIp")));
		uaLogLogin.setLoginFromWay(UtilTools.valueOf(paramMap.get("loginFromWay")));
		uaLogLogin.setLoginResult(onlineSatatus);
		int b = uaLogLoginDao.insertSelective(uaLogLogin);
		if( b != 1){
			rs.setFlag(false);
			rs.setCode(MacroDefine.JC_CODE.R_9999.getCode());
			rs.setMessage(MacroDefine.JC_CODE.R_9999.getMessage());
			return false;
		}
		
		return true;
	}

	public boolean after(Result<HashMap<String, Object>> rs) {
		HashMap<String,Object> respMap = new HashMap<String, Object>();
		respMap.put("exchangeId", uaMemberInfo.getExchangeId());
		respMap.put("distributorId", uaMemberInfo.getDistributorId());
		respMap.put("memberId", uaMemberInfo.getMemberId());
		respMap.put("loginName", uaMemberInfo.getLoginName());
		respMap.put("password", uaMemberInfo.getLoginPwd());
		respMap.put("lockStatus", uaMemberInfo.getLockStatus());
		respMap.put("onlineStatus", uaLoginMethod.getOnlineStatus());
		rs.setData(respMap);
		rs.setFlag(true);
		rs.setCode(MacroDefine.JC_CODE.S_0000.getCode());
		rs.setMessage(MacroDefine.JC_CODE.S_0000.getMessage());
		return true;
	}

}
